<template>
  <div class="MyFocus">
    <div class="top">
      <van-icon name="arrow-left" size="25" @click="onBack" />
      <div class="t">我的关注</div>
    </div>
    <div class="content">
      <div class="item" v-for="(item, index) in MyFocus" :key="index" @click="toUserDetails(item.userid)">
        <div class="left">
          <img :src="item.avatar_medium" alt="" />
        </div>
        <div class="center">
          <div class="name">
            {{ item.nick
            }}<span class="lvl"
              ><i>LV.{{ item.lvl }}</i></span
            >
          </div>
          <div class="signature">{{ item.introduction }}</div>
        </div>
        <div class="right" @click.stop="cancelFocus(item.userid,index)" ref="r">已关注</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "MyFocus",
  data() {
    return {
      MyFocus: [],
      users: [],
      userData: {
        phone: "",
        password: "",
        isLogin: false,
        Focus: [],
        nickName: "",
        portrait: "",
        gender: 1,
        location: "",
        signature: "",
      },
    };
  },
  created() {
    if (localStorage.getItem("users")) {
      this.users = JSON.parse(localStorage.getItem("users"));
      this.users.forEach((ele) => {
        for (let key in this.userData) {
          this.userData[key] = ele[key];
        }
      });
    }
    this.getMyFocus();
  },
  methods: {
    onBack() {
      this.users.forEach(ele=>{
          if(ele.phone == this.userData.phone){
              for(let key in ele){
                  ele[key] = this.userData[key]
              }
          }
      }) 
      localStorage.setItem('users',JSON.stringify(this.users)) 
      this.$router.go(-1);
    },
    getMyFocus() {
      this.userData.Focus.forEach((ele) => {
        this.axios({
          method: "get",
          url: "user/info",
          params: {
            userid: ele,
          },
        }).then((result) => {
          if (result.data.state == "success") {
            this.MyFocus.push(result.data.result.user);
          }
        });
      });
    },
    toUserDetails(id){
        this.$router.push({name:'UserDetails',query:{id}})
    },
    cancelFocus(id,index){
        if(this.$refs.r[index].innerHTML == '已关注'){
            this.$refs.r[index].innerHTML = '关注'
            this.$refs.r[index].style.background = '#fbb705'
            this.userData.Focus.forEach((ele,i)=>{
                if(ele == id){
                    this.userData.Focus.splice(i,1)
                }
            })
        }else{
            this.$refs.r[index].innerHTML = '已关注'
            this.$refs.r[index].style.background = '#eee'
            this.userData.Focus.push(id)
        }
    }
  },
};
</script>

<style lang="scss" scoped>
.MyFocus {
  .top {
    width: calc(100% - 20px);
    padding: 40px 10px 10px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #eee;
    background-color: #fbb705;
    .t {
      font-size: 20px;
      margin-left: 40px;
    }
  }
  .content {
    position: fixed;
    top: 77px;
    left: 0;
    right: 0;
    bottom: 0;
    .item {
      width: calc(100% - 40px);
      padding: 10px 20px;
      display: flex;
      align-items: center;
      background-color: #fff;
      .left {
        img {
          width: 30px;
          height: 30px;
          border-radius: 50%;
        }
      }
      .center {
          width: 195px;
        margin-left: 15px;
        .name {
          .lvl {
            margin-left: 5px;
            color: #fbb705;
            font-weight: 600;
          }
        }
        .signature {
          color: #999;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
        }
      }
      .right {
          width: 80px;
          text-align: center;
        margin-left: 25px;
        background-color: #eee;
        padding: 8px 0;
        border-radius: 20px;
      }
    }
  }
}
</style>